package com.datoucai.config;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class DynamicThreadFactory implements ThreadFactory {
    private final String namePrefix;
    private final AtomicInteger counter = new AtomicInteger(1);

    public DynamicThreadFactory(String poolName) {
        this.namePrefix = poolName + "-";
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(r, namePrefix + counter.getAndIncrement());
        thread.setDaemon(true); // 设置为守护线程
        thread.setPriority(Thread.NORM_PRIORITY);
        thread.setUncaughtExceptionHandler((t, e) ->
            log.error("动态线程池-线程异常: " + t.getName() + ", 错误: " + e));
        return thread;
    }
}

